

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Created by wangyg 
 * on 2017/3/21 19:14
 */
public class Json {

	public static String string = "{\n"
			+ "\t\"ContractRoot\":{\n"
			+ "\t\t\"SvcCont\":{\n"
			+ "\t\t\t\"certPhoneNumRel\":[\n"
			+ "\t\t\t\t{\n"
			+ "\t\t\t\t\t\"actionType\":\"12\",\n"
			+ "\t\t\t\t\t\"certAddress\":\"北京市西城区南滨河路27号\",\n"
			+ "\t\t\t\t\t\"certNum\":\"412725198912084711\",\n"
			+ "\t\t\t\t\t\"certType\":\"41\",\n"
			+ "\t\t\t\t\t\"custName\":\"张山\",\n"
			+ "\t\t\t\t\t\"gPartyId\":\"\",\n"
			+ "\t\t\t\t\t\"lanId\":\"8510100\",\n"
			+ "\t\t\t\t\t\"phoneNum\":\"18901230122\",\n"
			+ "\t\t\t\t\t\"serviceType\":\"1000\"\n"
			+ "\t\t\t\t},\n"
			+ "\t\t\t\t{\n"
			+ "\t\t\t\t\t\"actionType\":\"10\",\n"
			+ "\t\t\t\t\t\"certAddress\":\"北京市西城区南滨河路27号\",\n"
			+ "\t\t\t\t\t\"certNum\":\"412725198912084711\",\n"
			+ "\t\t\t\t\t\"certType\":\"123\",\n"
			+ "\t\t\t\t\t\"custName\":\"张山\",\n"
			+ "\t\t\t\t\t\"gPartyId\":\"\",\n"
			+ "\t\t\t\t\t\"lanId\":\"8510100\",\n"
			+ "\t\t\t\t\t\"phoneNum\":\"18901230124\",\n"
			+ "\t\t\t\t\t\"serviceType\":\"1000\"\n"
			+ "\t\t\t\t}\n"
			+ "\t\t\t],\n"
			+ "\t\t\t\"channelNbr\":\"9871220000\",\n"
			+ "\t\t\t\"commonRegionId\":\"8510100\",\n"
			+ "\t\t\t\"staffCode\":\"1\",\n"
			+ "\t\t\t\"traceId\":\"1000000201201112041000000012\"\n"
			+ "\t\t},\n"
			+ "\t\t\"TcpCont\":{\n"
			+ "\t\t\t\"AppKey\":\"1000000201\",\n"
			+ "\t\t\t\"Method\":\"order.party.modCertPhoneNumRel\",\n"
			+ "\t\t\t\"ReqTime\":\"20130817200202123\",\n"
			+ "\t\t\t\"Sign\":\"123\",\n"
			+ "\t\t\t\"TransactionID\":\"1000000201201112041000000011\",\n"
			+ "\t\t\t\"Version\":\"V1.0\"\n"
			+ "\t\t}\n"
			+ "\t}\n"
			+ "}\n";

	public static void main(String[] args) {
		excute();
//		System.out.println(excute.toString());

	}

	public static void excute() {
		//处理入参
		Map<String, Object> map = new HashMap<String, Object>();
		String sysCode = "";
		String transId = "";

		JSONObject mjo = JSONObject.fromObject(string);
		JSONObject svccont = null;
		if (mjo.containsKey("ContractRoot")) {
			transId = mjo.getJSONObject("ContractRoot").getJSONObject("TcpCont").getString("TransactionID");
			svccont = mjo.getJSONObject("ContractRoot").getJSONObject("SvcCont");
		} else {
			if (mjo.containsKey("TransactionID")) {
				transId = mjo.getString("TransactionID");
			} else if (mjo.containsKey("transId")) {
				transId = mjo.getString("transId");
			}
		}

		String certNum = "430101";
		String certType = "99";
		//JSONArray jaas= new JSONArray();
		JSONArray ja = null;
		if (svccont.containsKey("certPhoneNumRel")) {
//			 ja= JSONArray.fromObject(svccont.get("certPhoneNumRel"));
			 ja = mjo.getJSONArray("certPhoneNumRel");
			//JSONObject jo = null;

			for (int i = 0; i < ja.size(); i++) {
				JSONObject jaa = ja.getJSONObject(i);
				certType = jaa.getString("certType");
				certNum = jaa.getString("certNum");
				if (Arrays.asList("1", "12", "41").contains(certType)) {
					jaa.element("certType", "1");
				}
				//jaas.add(i, jaa);
				//jaas.a
			}
			//			svccont.element("certPhoneNumRel", ja);
			//jo.element("certPhoneNumRel", jaas);
		}/* else if (svccont.containsKey("certPhoneNumRelList")){
			//JSONArray ja = JSONArray.fromObject(mjo.get("certPhoneNumRelList"));
			ja = mjo.getJSONArray("certPhoneNumRel");
			//JSONObject ja0 = ja.getJSONObject(0);
			//certType = JsonUtil.getStringFromJSON(ja0,"certType","");
			//certNum = JsonUtil.getStringFromJSON(ja0,"certNum","");
			for(int i =0;i<ja.size();i++){
				JSONObject jaa = ja.getJSONObject(i);
				certType = jaa.getString("certType");
				certNum = jaa.getString("certNum");
				if(Arrays.asList("1","12","41").contains(certType)){
					jaa.element("certType", "1");
				}
			}
		} else {
			certType = svccont.getString("certType");
			if(Arrays.asList("1","12","41").contains(certType)){
				certType="1";
			}
//			certNum = svccont.getString("certNum");
		}*/

		//		if(certType==null || certType.equals("")){
		//			certType ="99";
		//		}
		//		if(certNum==null || certNum.equals("")){
		//			certNum = "430101199001010001";
		//		}
//		System.out.println(mjo.toString());
//		svccont.element("certPhoneNumRel", ja);
//		mjo.getJSONObject("ContractRoot").element("SvcCont", svccont);
		// 入参必须包括  证件类型和证件号
		//		map.put("certNumDb", certNum);
		//		map.put("certTypeDb",certType);
		//		map.put("systemId", sysCode);
		//		map.put("transId", transId);
		//		map.put("body", (JSONObject)mjo);
		//
		//		return map;
//		return mjo;

	}
}
